package co.com.smart.eagle.client.view.impl.smartcelltable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.cell.client.AbstractInputCell;
import com.google.gwt.cell.client.SelectionCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.SelectElement;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

public class SmartSelectionCell extends  AbstractInputCell<String, String> {

	interface Template extends SafeHtmlTemplates {
		@Template("<option value=\"{0}\" style='width:95%;'>{0}</option>")
		SafeHtml deselected(String option);

		@Template("<option value=\"{0}\" selected=\"selected\" style='width:95%;'>{0}</option>")
		SafeHtml selected(String option);
	}

	private static Template template;
	private Boolean enable = true;

	private HashMap<ObjetoSelectCell, Integer> indexForOption = new HashMap<ObjetoSelectCell, Integer>();

	private final List<ObjetoSelectCell> options;
	private ObjetoSelectCell currentValue;

	/**
	 * Construct a new {@link SelectionCell} with the specified options.
	 * 
	 * @param options
	 *            the options in the cell
	 */
	public SmartSelectionCell(List<ObjetoSelectCell> options) {
		super("change");
		if (template == null) {
			template = GWT.create(Template.class);
		}
		this.options = new ArrayList<ObjetoSelectCell>(options);
		int index = 0;
		for (ObjetoSelectCell option : options) {
			indexForOption.put(option, index++);
		}
	}
	
	public SmartSelectionCell(List<ObjetoSelectCell> options, boolean enable) {
		super("change");
		this.enable = enable;
		if (template == null) {
			template = GWT.create(Template.class);
		}
		this.options = new ArrayList<ObjetoSelectCell>(options);
		int index = 0;
		for (ObjetoSelectCell option : options) {
			indexForOption.put(option, index++);
		}
	}

	@Override
	public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event,
			ValueUpdater<String> valueUpdater) {
		super.onBrowserEvent(context, parent, value, event, valueUpdater);
		String type = event.getType();
		if ("change".equals(type)) {
			Object key = context.getKey();
			SelectElement select = parent.getFirstChild().cast();
			String newValue = options.get(select.getSelectedIndex()).getCodigo();
			currentValue = options.get(select.getSelectedIndex());
			setViewData(key, newValue);
			finishEditing(parent, newValue, key, valueUpdater);
			if (valueUpdater != null) {
				valueUpdater.update(newValue);
			}
		}
	}

	@Override
	public void render(Context context, String value, SafeHtmlBuilder sb) {
		// Get the view data.
		Object key = context.getKey();
		String viewData = getViewData(key);
		if (viewData != null && viewData.equals(value)) {
			clearViewData(key);
			viewData = null;
		}

		int selectedIndex = getSelectedIndex(viewData == null ? value : viewData);
		if ( enable ){
			sb.appendHtmlConstant("<select tabindex=\"-1\">");	
		}else{
			sb.appendHtmlConstant("<select tabindex=\"-1\" disabled=\"disabled\">");
		}
		
		int index = 0;
		for (ObjetoSelectCell option : options) {
			if (index++ == selectedIndex) {
				sb.append(template.selected(option.getDescripcion()));
			} else {
				sb.append(template.deselected(option.getDescripcion()));
			}
		}
		sb.appendHtmlConstant("</select>");
	}

	private int getSelectedIndex(String value) {
		ObjetoSelectCell objetoSelectCell;
		for (Map.Entry<ObjetoSelectCell, Integer> entry : indexForOption.entrySet()) {
			objetoSelectCell = (ObjetoSelectCell) entry.getKey();
			if (objetoSelectCell.getCodigo().equals(value)) {
				return (int) entry.getValue();
			}
		}
		return -1;
	}

	@Override
	public void setValue(com.google.gwt.cell.client.Cell.Context context, Element parent, String value) {
		super.setValue(context, parent, value);
	}

	public ObjetoSelectCell getCurrentValue() {
		return currentValue;
	}

	public void setCurrentValue(ObjetoSelectCell currentValue) {
		this.currentValue = currentValue;
	}

	public Boolean getEnable() {
		return enable;
	}

	public void setEnable(Boolean enable) {
		this.enable = enable;
	}
	
	
}
